/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util.pool;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.telnet.EthzSSH;

/**
 * <p>
 * Description: 利用commons-pool 重新实现SSH2连接池
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-10-23 上午9:40:50          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class SSH2ConnPool {

    private static final Logger logger = Logger.getLogger(SSH2ConnPool.class);

    private final Map<String, ObjectPool> map = new HashMap<String, ObjectPool>();

    private static SSH2ConnPool instance = null;

    private static int outTime = 60 * 1000;

    private final Object _lock = new Object();

    private SSH2ConnPool() {

    }

    public synchronized static SSH2ConnPool getInstance() {
        if (instance == null) {
            instance = new SSH2ConnPool();
        }
        return instance;
    }

    /**
     * 连接池本身有个超时时间
     * @param host
     * @param port
     * @param user
     * @param pwd
     * @param maxSize
     * @return
     * @throws Exception
     */
    public EthzSSH getConnection(String host, int port, String user, String pwd, int maxSize) throws Exception {
        ObjectPool pool = map.get(host);
        if (pool == null) {
            synchronized (_lock) {
                pool = map.get(host);
                if (pool == null) {
                    SSH2PoolableObject factory = new SSH2PoolableObject(host, port, user, pwd);
                    GenericObjectPool.Config config = new GenericObjectPool.Config();
                    config.lifo = false;
                    config.maxActive = maxSize;
                    config.maxIdle = maxSize;
                    config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
                    config.minIdle = 1;
                    config.testOnReturn = true;
                    config.testOnBorrow = true;
                    config.maxWait = outTime;
                    pool = new GenericObjectPool(factory, config);
                    map.put(host, pool);
                }
            }
        }

        return (EthzSSH) pool.borrowObject();
    }

    public void updateUser(String ip) {
        ObjectPool pool = map.get(ip);
        try {
            logger.info("[TIP]\t重建连接池...");
            pool.close();
            map.put(ip, null);
        } catch (Exception e) {
            logger.error(e);
        }

    }

    /**
     * 释放一个连接  如果此连接有效 则放入连接池 如果无效 则关闭
     * @param ip
     * @param ssh2
     */
    public void free(String ip, EthzSSH ssh2) {
        ObjectPool pool = map.get(ip);
        try {
            if (ssh2 == null) {
                return;
            }
            pool.returnObject(ssh2);
        } catch (Exception e) {
            logger.error(e);
        }
    }

}
